/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

package app.controllers.merchant;

import app.Const;
import app.interceptors.merchant.CommonInterceptor;
import app.models.booking.KtvBooking;
import com.github.sog.controller.AjaxMessage;
import com.github.sog.controller.BasicController;
import com.github.sog.kit.date.DateProvider;
import com.github.sog.libs.AppFunc;
import com.google.common.base.Strings;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import org.joda.time.DateTime;

import java.util.Date;

/**
 * <p>
 * .
 * </p>
 *
 * @author sagyf yang
 * @version 1.0 2014-05-25 12:07
 * @since JDK 1.6
 */
@Before(CommonInterceptor.class)
public class KtvController extends BasicController {

    /**
     * The index route.
     */
    public void index() {
        String start_date = getPara("start_date");
        String end_date = getPara("end_date");

        String code = getPara("code");
        String phone = getPara("phone");
        int page = getParaToInt("page", 1);

        if (Strings.isNullOrEmpty(start_date)) {
            DateTime now = DateTime.now();
            start_date = now.toString(AppFunc.DATE_TIME_PATTERN_YYYY_MM_DD);
        }

        if (Strings.isNullOrEmpty(end_date)) {
            DateTime now = DateTime.now();
            end_date = now.toString(AppFunc.DATE_TIME_PATTERN_YYYY_MM_DD);
        }

        /*DateTime start = DateTime.parse(start_date + " 00:00:00", AppFunc.DATE_TIME_PATTERN_YYYY_MM_DD_HH_MM_SS);
        DateTime end = DateTime.parse(start_date + " 23:59:59", AppFunc.DATE_TIME_PATTERN_YYYY_MM_DD_HH_MM_SS);*/

        Page<KtvBooking> bookingPage = KtvBooking.dao.findByDateAndCodeAndPhone(start_date, end_date, code, phone, page);
        setAttr("bookingPage", bookingPage);
        setAttr("bookings", bookingPage.getList());
        setAttr("total", bookingPage.getTotalPage());

        setAttr("start_date", start_date);
        setAttr("end_date", end_date);

        setAttr("code", code);
        setAttr("phone", phone);
        setAttr("page", page);
        render("index.ftl");
    }

    public void ok() {
        long booking_id = getParaToLong(0, 0l);
        if (booking_id == 0) {
            renderJson(AjaxMessage.nodata());
        } else {
            KtvBooking booking = KtvBooking.dao.findById(booking_id);
            booking.set(Const.COLUMN_STATUS, Const.BOOKING_STATUS.STATUS_2);
            booking.set("ok_time", DateProvider.DEFAULT.getCurrentDate());
            booking.update();
            renderSuccess();
        }
    }
}